package com.example.as_sum.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.as_sum.R;
import com.example.as_sum.entity.ShopInfo;
import com.example.as_sum.glide.GlideUtil;

import java.util.List;

/**
 * Created by ran on 2016/8/23.
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHodler> {
    private Context context;
    private List<ShopInfo> shopInfos;
    private boolean isAllShow;
    public RecyclerViewAdapter(Context context, List<ShopInfo> shopInfos) {
        this.context = context;
        this.shopInfos = shopInfos;
        this.isAllShow=false;
    }
    public void setAllShow(boolean isShow){
        this.isAllShow=isShow;
        notifyDataSetChanged();
    }
    public void updateData(List<ShopInfo> shopInfos){
        this.shopInfos=shopInfos;
//        notifyDataSetChanged();
    }
    @Override
    public RecyclerViewAdapter.MyViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.shop_item,parent,false);
        MyViewHodler hodler=new MyViewHodler(view);
        return hodler;
    }

    @Override
    public void onBindViewHolder(final RecyclerViewAdapter.MyViewHodler holder, int position) {
        ShopInfo shopInfo=shopInfos.get(position);
        if(isAllShow){
            holder.img_select.setVisibility(View.VISIBLE);
            if(shopInfo.isState()){
                holder.img_select.setImageResource(R.mipmap.icon_vedio_select);
            }else{
                holder.img_select.setImageResource(R.mipmap.icon_vedio_unselect);
            }
        }else{
            holder.img_select.setVisibility(View.GONE);
        }
        holder.tv_name.setText(shopInfo.getSname());
        holder.img_dz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.img_dz.setImageResource(R.drawable.dz_true);
                Animation animation=AnimationUtils.loadAnimation(context,R.anim.anim_dz);
                holder.img_dz.startAnimation(animation);
            }
        });
        holder.img_sc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.img_sc.setImageResource(R.drawable.sc_true);
                Animation animation=AnimationUtils.loadAnimation(context,R.anim.anim_dz);
                holder.img_sc.startAnimation(animation);
            }
        });
        holder.img_sc2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.img_sc2.setImageResource(R.drawable.sc2_true);
                Animation animation=AnimationUtils.loadAnimation(context,R.anim.anim_dz);
                holder.img_sc2.startAnimation(animation);
            }
        });
        GlideUtil.loadImage(context,shopInfo.getImg(),holder.img_bg);
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public long getItemId(int position) {
        return shopInfos.get(position).getSid();
    }

    @Override
    public int getItemCount() {
        return shopInfos.size();
    }
    class MyViewHodler extends RecyclerView.ViewHolder{
        ImageView img_dz,img_sc,img_sc2,img_select,img_bg;
        TextView tv_name;
        public MyViewHodler(View itemView) {
            super(itemView);
            img_bg=(ImageView)itemView.findViewById(R.id.shop_item_img);
            img_select=(ImageView)itemView.findViewById(R.id.shop_item_sele);
            tv_name=(TextView)itemView.findViewById(R.id.shop_item_text);
            img_dz=(ImageView)itemView.findViewById(R.id.shop_item_dz);
            img_sc=(ImageView)itemView.findViewById(R.id.shop_item_sc);
            img_sc2=(ImageView)itemView.findViewById(R.id.shop_item_sc2);
        }
    }
}
